var express = require('express');
const { json } = require('express/lib/response');
var router = express.Router();
const hotPlayService = require('../service/hotPlayService.js');

router.get('/all', async function (req, res) {
    let data = await hotPlayService.findAll();
    res.send({
        data
    })
})

//查询
router.get('/', async function (req, res) {
    let { type, value, current, pageSize, films } = req.query;
    let searchObj = {};
    let data = await hotPlayService.find(searchObj, {
        current: current || 1, pageSize: pageSize || 5
    });
    if (value) {
        console.log(type, value);
        console.log(data.rows);
        data = data.rows.filter((item) => {
            if (item.films) {
                return item.films[type].search(value) >= 0
            } else {
                return item.films;
            }
        });
        res.send({
            data: {
                current, pageSize, rows: data, total: data.length
            }
        })
        return;
    }
    res.send({ data })
});
//增加
router.post('/', async function (req, res) {
    let { films } = req.body;
    films = JSON.parse(films);
    console.log('films', films);
    for (let i = 0; i < films.length; i++) {
        await hotPlayService.insert({ films: films[i] });

    }
    res.send({ status: 1 });
}); 1
//删除
router.delete('/:id', async function (req, res) {
    let { id } = req.params;
    let data = await hotPlayService.remove(id);
    console.log(data);
    res.send({ data });
});

router.get('/:id', async function (req, res) {
    let { id } = req.params;
    let data = await hotPlayService.findById(id);
    console.log(data);
    res.send({ data });
});
module.exports = router;